//
//  BindPhoneViewController.m
//  tryshopping.boss
//
//  Created by Mr.Lee on 2017/1/4.
//  Copyright © 2017年 Reabam. All rights reserved.
//

#import "BindPhoneViewController.h"
#import "IQKeyboardManager.h"

@interface BindPhoneViewController ()
// 下一步按钮
@property (weak, nonatomic) IBOutlet UIButton *bindPhoneBtn;
// 新手机号输入框
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
// 新手机验证码
@property (weak, nonatomic) IBOutlet UITextField *VCodeTF;
// 获取验证码按钮
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@end

@implementation BindPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"改绑手机号";
    self.view.backgroundColor = zuiqianhuiColor;
    
    // 设置下一步按钮
    self.bindPhoneBtn.layer.cornerRadius = 4.0;
    self.bindPhoneBtn.layer.masksToBounds = YES;
    
    // 设置获取验证码按钮
    self.getCodeBtn.layer.cornerRadius = 4.0;
    self.getCodeBtn.layer.masksToBounds = YES;
    
    
    // 是否输入手机号的通知
    [self.phoneTF addTarget:self action:@selector(phoneTFChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.bindPhoneBtn setBackgroundColor:ZhuTibtn];
    self.bindPhoneBtn.enabled = YES;
}



// 获取验证码
- (IBAction)VerificationCodeBtn:(UIButton *)sender {
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    if ([_phoneTF.text length]<1)
        {
            [TSAlertTool showAlertMessage:@"请输入手机号码"];
        }else{
            if ([_phoneTF.text length]!=11)
            {
                [TSAlertTool showAlertMessage:@"手机位数有误"];
            }else{
            
                __block int timeout=60; //倒计时时间
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
                dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
                dispatch_source_set_event_handler(_timer, ^{
                    if(timeout<=0){  //倒计时结束，关闭
                        dispatch_source_cancel(_timer);
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //设置界面的按钮显示 根据自己需求设置
                            [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
                            sender.userInteractionEnabled = YES;
                        });
                    }else{
                        int seconds = timeout % 60;
                        NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //设置界面的按钮显示 根据自己需求设置
                            //                NSLog(@"____%@",strTime);
                            if (strTime.intValue==59) {
                                NSLog(@"____%@",strTime);
                                NSMutableDictionary *params = [NSMutableDictionary new];
                                [params setValue:self.phoneTF.text forKey:@"mobile"];
                                [params setValue:@"R" forKey:@"msgType"];
//                                DLog(@"%@", params);
                                [HttpRequest requestData1:@"Common/SendMsg" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                                    if (isSuccess) {
                                        NSLog(@"====%@",severDataDict.dataDict);
                                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                                            //                                        [MBProgressHUD hideProgressHUDInternal:self];
                                            //                                        NSString*ResultString=[severDataDict.dataDict objectForKey:@"ResultString"];
                                            //                                        [self showAlertMessage:ResultString];
                                            [MBProgressHUD showTextHudSub:@"已成功获取验证码" view:self.view];
                                            
                                            
                                        }else{
                                            //                                [MBProgressHUD hideProgressHUDInternal:self.view];
                                            [MBProgressHUD showTextHudSub:@"获取验证码失败" view:self.view];
                                        }}
                                    else
                                    {
                                        [MBProgressHUD hideProgressHUDInternal:self.view];
                                        [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
                                    }
                                }];
                            }
                            
                            if (![strTime isEqualToString:@"00"]) {
                                [sender setTitle:[NSString stringWithFormat:@"%@秒重获",strTime] forState:UIControlStateNormal];
                                
                            }
                            sender.userInteractionEnabled = NO;
                            
                        });
                        timeout--;
                        
                    }
                });
                dispatch_resume(_timer);
            
            
              }
    
          }
    
}


- (IBAction)updataPhoneNum:(UIButton *)sender {
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    
    if ([_phoneTF.text length]<1)
    {
        [TSAlertTool showAlertMessage:@"请输入手机号码"];
    }  else  {
        if ([self.VCodeTF.text length] <1)
        {
            [TSAlertTool showAlertMessage:@"请输入验证码"];;
        } else {
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* tokenId = [user objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:tokenId forKey:@"tokenId"];
            [params setValue:self.phoneTF.text forKey:@"ucode"];  // 新手机账号
            [params setValue:self.VCodeTF.text forKey:@"mcode"];  // 短信验证码
            [params setValue:self.vcodeStr forKey:@"vcode"];      // 安全验证码
            
            NSLog(@"====%@",params);
            
            [HttpRequest requestData1:@"Common/UpdatePhone" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    NSLog(@"====%@",severDataDict.dataDict);
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [MBProgressHUD hideProgressHUDInternal:self.view];
                        
                        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                        [user setObject:@"out" forKey:@"tokenId"];
                        [user setObject:self.phoneTF.text forKey:@"userNameTextField"];
                        [user synchronize];
                        
                        
                        LoginViewController* Login=[[LoginViewController alloc]init];
                        [self.navigationController pushViewController:Login animated:YES];
                        
                        
                    }else{
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                    }
                }
                else
                {
                    [MBProgressHUD hideProgressHUDInternal:self.view];
                    [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
                }
            }];
            

        }
    }

}

- (void)phoneTFChanged:(UITextField *)tf{
    //账号长度限制
    if ([tf.text length] > 11)
    {
        tf.text = [tf.text substringToIndex:11];
    }
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}
@end
